package com.th.contraller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.th.dao.HistoryDao;
import com.th.dao.ObjDaoImp;
import com.th.entity.Obj;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.validation.ObjectError;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
@WebServlet(name = "AdmMag", urlPatterns = {"/showApplyUp","/showApplyGet","/agreeUpObj","/agreeGetObj","/history","/historyByDate","/historyByKey"})
public class AdmMag extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri = request.getRequestURI();
        try {
            Method method = AdmMag.class.getDeclaredMethod(uri.substring(uri.lastIndexOf("/")+1,uri.length()),HttpServletRequest.class,HttpServletResponse.class);
            method.invoke(this,request,response);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri = request.getRequestURI();
        try {
            Method method = AdmMag.class.getDeclaredMethod(uri.substring(uri.lastIndexOf("/")+1,uri.length()),HttpServletRequest.class,HttpServletResponse.class);
            method.invoke(this,request,response);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    //管理员同意申请
    public void agreeUpObj(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        ObjectMapper mapper = new ObjectMapper();
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        if(request.getSession().getAttribute("adm") == null)
        {
            String error = "管理员未登录,请登录后操作";
            out.print(mapper.writeValueAsString(error));
            return;
        }
        ApplicationContext context = new ClassPathXmlApplicationContext("dao.xml");
        ObjDaoImp objDaoImp = context.getBean("objDaoImp",ObjDaoImp.class);
        objDaoImp.agreeUpObj(Integer.parseInt(request.getParameter("get_id")));
    }

    //管理员同意领取
    public void agreeGetObj(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        ObjectMapper mapper = new ObjectMapper();
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        if(request.getSession().getAttribute("adm") == null)
        {
            String error = "管理员未登录,请登录后操作";
            out.print(mapper.writeValueAsString(error));
            return;
        }
        ApplicationContext context = new ClassPathXmlApplicationContext("dao.xml");
        ObjDaoImp objDaoImp = context.getBean("objDaoImp",ObjDaoImp.class);
        objDaoImp.agreeGetObj(Integer.parseInt(request.getParameter("get_id")));
        HistoryDao  historyDao = context.getBean("historyDaoImp",HistoryDao.class);
        Obj obj = objDaoImp.selectObjById(Integer.parseInt(request.getParameter("get_id")));
        historyDao.insertHistory(obj);
        objDaoImp.deleteObj(obj.getGet_id());
    }

    //历史查询
    public void history(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        ObjectMapper mapper = new ObjectMapper();
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        if(request.getSession().getAttribute("adm") == null)
        {
            String error = "管理员未登录,请登录后操作";
            out.print(mapper.writeValueAsString(error));
            return;
        }
        Integer page = (Integer) request.getSession().getAttribute("page");
        ApplicationContext context = new ClassPathXmlApplicationContext("dao.xml");
        HistoryDao  historyDao = context.getBean("historyDaoImp",HistoryDao.class);
        out.print(mapper.writeValueAsString(historyDao.selectHistorys(page,10)));
    }

    //根据时间查询历史查询
    public void historyByDate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        ObjectMapper mapper = new ObjectMapper();
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        if(request.getSession().getAttribute("adm") == null)
        {
            String error = "管理员未登录,请登录后操作";
            out.print(mapper.writeValueAsString(error));
            return;
        }
        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");
        Integer page = (Integer) request.getSession().getAttribute("page");
        ApplicationContext context = new ClassPathXmlApplicationContext("dao.xml");
        HistoryDao  historyDao = context.getBean("historyDaoImp",HistoryDao.class);
        out.print(mapper.writeValueAsString(historyDao.selectHistoryByDate(page,10,startDate,endDate)));
    }

    //根据关键字查询历史查询
    public void historyByKey(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        ObjectMapper mapper = new ObjectMapper();
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        if(request.getSession().getAttribute("adm") == null)
        {
            String error = "管理员未登录,请登录后操作";
            out.print(mapper.writeValueAsString(error));
            return;
        }
        String key = request.getParameter("key");
        Integer page = (Integer) request.getSession().getAttribute("page");
        ApplicationContext context = new ClassPathXmlApplicationContext("dao.xml");
        HistoryDao  historyDao = context.getBean("historyDaoImp",HistoryDao.class);
        out.print(mapper.writeValueAsString(historyDao.selectHistoryByKey(page,10,key)));
    }

    //查看申请上传
    public void  showApplyUp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        ObjectMapper mapper = new ObjectMapper();
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        if(request.getSession().getAttribute("adm") == null)
        {
            String error = "管理员未登录,请登录后操作";
            out.print(mapper.writeValueAsString(error));
            return;
        }
        Integer page = Integer.parseInt(request.getParameter("page"));
        ApplicationContext context = new ClassPathXmlApplicationContext("dao.xml");
        ObjDaoImp objDaoImp = context.getBean("objDaoImp",ObjDaoImp.class);
        out.print(mapper.writeValueAsString(objDaoImp.selectObjs(page,10,0)));
    }

    //查看申请领取
    public void  showApplyGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        ObjectMapper mapper = new ObjectMapper();
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        if(request.getSession().getAttribute("adm") == null)
        {
            String error = "管理员未登录,请登录后操作";
            out.print(mapper.writeValueAsString(error));
            return;
        }
        Integer page = Integer.parseInt(request.getParameter("page"));
        ApplicationContext context = new ClassPathXmlApplicationContext("dao.xml");
        ObjDaoImp objDaoImp = context.getBean("objDaoImp",ObjDaoImp.class);
        out.print(mapper.writeValueAsString(objDaoImp.selectObjs(page,10,2)));
    }
}